#include "main.h"

int socket_fd;
char *mac_ptr;
char mac_buf[6] = {};
extern sqlite3 *fsl_db;

#define MAC_ADDRESS_LENGTH 6


int main(int argc, char *argv[])
{
    if (argc != 2) 
    {
            printf("Usage: %s <port>\n", argv[0]);
            return 1;
    }


    uint8_t result = get_mac_address("wlan0", mac_buf);
    if (result != 0) 
    {
        FSL_LOG("Failed to get MAC address of wlan0 \n");
        return 0;
    }

    // for(uint16_t i = 0; i < 6; i++)
    // {
    //     printf("%02x ", mac_buf[i]);
    // }
    // printf("\n");

    mac_ptr = (char *)malloc(MAC_ADDRESS_LENGTH * 2 + 1 + MAC_ADDRESS_LENGTH * 4); // 一个字节对应两个十六进制字符，再加一个字节用于存放空字符
    if (mac_ptr == NULL) 
    {
        FSL_LOG("内存分配失败\n");
        return 1;
    }
    mac_ptr[MAC_ADDRESS_LENGTH * 2] = '\0';

    // 将 mac_buf 中的数据转换为十六进制字符串，并赋值给 mac_ptr
    sprintf(mac_ptr, "%02x%02x%02x%02x%02x%02x", mac_buf[0], mac_buf[1], mac_buf[2], mac_buf[3], mac_buf[4], mac_buf[5]);


    char ip_address[16] = "127.0.0.1";
    // char ip_address[16];

    // if (get_ip_address("wlan0", ip_address) == 0) 
    // {
    //     printf("%s IP address of wlan0: %s\n", TAG, ip_address);
    // }
    // else 
    // {
    //     printf("%s Failed to get IP address of wlan0\n", TAG);
    // }
#ifdef FSL_MQTT_
    FSL_Mqtt_Init();
#endif

    sqlite3_open_database(&fsl_db);
    sqlite3_create_table(fsl_db);

#ifdef FSL_SOCKET
    struct sockaddr_in netaddr;
    struct sockaddr_in fromaddr;
    socklen_t len = sizeof(struct sockaddr);

    memset(&netaddr, 0, len);
    netaddr.sin_family = AF_INET;
    netaddr.sin_port = htons(atoi(argv[1]));
    netaddr.sin_addr.s_addr = inet_addr(ip_address);
    FSL_LOG("My IP Adress: %s", inet_ntoa(netaddr.sin_addr));
    FSL_LOG("My Port: %d", ntohs(netaddr.sin_port));


    if((socket_fd = socket(AF_INET,SOCK_DGRAM,0)) < 0)
    {
        perror("Fail to create socket");
        return -1;
    }

    // int iSockOptVal = 1;
    // if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &iSockOptVal, sizeof(iSockOptVal)) == -1)
    // {
    //     perror("setsockopt fail");
    //     close(socket_fd);
    //     exit(EXIT_FAILURE);
    // } 


    if (bind(socket_fd, (struct sockaddr*)&netaddr, sizeof(struct sockaddr)) == -1) {
        perror("bind");
        exit(1);
    }

    char buf[100] = {};
    FSL_LOG("UDP Server is running...");

    while (1) 
    {
        int ret = recvfrom(socket_fd, buf, 100, 0, (struct sockaddr *)&fromaddr, &len);
        if (ret == -1) 
        {
            perror("recvfrom");
            exit(1);
        }

        FSL_LOG("From Client IP: %s, Port: %d", inet_ntoa(fromaddr.sin_addr), ntohs(fromaddr.sin_port));
        printf("Received HEX: ");
        for (int i = 0; i < ret; i++) 
        {
            printf("%02X ", (unsigned char)buf[i]);
        }
        printf("\n");
        buf[ret] = '\0';

        FSL_Handle_Protocol_Data(M_DWORD_WORD_COMBINE(buf[0], buf[1]), M_DWORD_WORD_COMBINE(buf[3], buf[2]), &fromaddr, buf);
        
        FSL_LOG("next=====================================================================");
        memset(buf, 0, sizeof(buf));
    }

    close(socket_fd);
#endif
}

